import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import StudentView from '@/views/StudentView.vue'
import DepartmentView from '@/views/DepartmentView.vue'
import IndexView from '@/views/IndexView.vue'
import TeacherView from '@/views/TeacherView.vue'
import LoginView from '@/views/LoginView.vue'
import CourseView from '@/views/CourseView.vue'


import LoginStudentView from '@/views/LoginStudentView.vue'
import SingleView from '@/views/SingleView.vue'
import WelcomeView from '@/views/WelcomeView.vue'
import ChooseView from '@/views/ChooseView .vue'
import LookView from '@/views/LookView.vue'
import { useTokenStore } from '@/stores/token.js';//使用store
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    //管理员登录页面
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    //学生登录页面
    {
      path: '/loginStudent',
      name: 'loginStudent',
      component: LoginStudentView
    },
    //管理员页面
    {
      path: '/',
      name: 'home',
      component: HomeView,
      //重定向
      redirect: '/login',
      children: [//子路由，访问子路由时，父路由的内容也会一同渲染
        {
          path: '/stu',
          component: StudentView
        }, {
          path: '/dept',
          component: DepartmentView
        }, {
          path: '/index',
          component: IndexView
        }, {
          path: '/teach',
          component: TeacherView
        }, {
          path: '/course',
          component: CourseView
        }
      ]
    },
    //学生登陆页面
    {
      path: '/',
      name: 'single',
      component: SingleView,
      // 重定向
      redirect: '/welcome',
      children: [//子路由，访问子路由时，父路由的内容也会一同渲染
        {
          path: '/welcome',
          component: WelcomeView
        }, {
          path: '/choose',
          component: ChooseView
        }, {
          path: '/look',
          component: LookView
        }
      ]
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue')
    }
  ]
})

//导航守卫,避免在登录页面直接访问里边的页面
//to 要访问的位置  from 起始位置
router.beforeEach((to, from) => {
  if (to.path == "/login" || to.path == "/loginStudent") {
    return true;
  } else {
    const tokenStore = useTokenStore();
    if (tokenStore.tokenStr==null) {
      return '/login';
    } else {
      return true;
    }
  }
})

export default router
